﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Services.Common;
using RssAgregatorBusiness;
using System.ServiceModel.Syndication;
using System.IO;
using System.Collections.ObjectModel;

namespace RSSAggregator_WebRole
{
    [DataServiceKey("PartitionKey", "RowKey")]
    public class Feed : Microsoft.Samples.ServiceHosting.StorageClient.TableStorageEntity
    {
        #region Fields
        private DateTime creationDate;
        private string title;
        private string category;
        private string description;
        private int score;
        private List<RssLink> rssUrls;
        private int subscribers;


        #endregion

        #region Properties
        public DateTime CreationDate
        {
            get { return creationDate; }
        }
        
        public string Title
        {
            get { return this.title; }
            set { this.title = value; }
        }

        public string Category
        {
            get { return this.category; }
            set { this.category = value; }
        }

        public string Description
        {
            get { return this.description; }
            set { this.description = value; }
        }

        public int Score
        {
            get { return this.score; }
            set { this.score = value; }
        }

        public int Subscribers
        {
            get { return this.subscribers; }
            set { this.subscribers = value; }
        }

        #endregion

        public Feed() : this("unspecified","unknown")
        {
            Init();
        }

        public Feed(string t, string c)
        {
            this.Title = t;
            this.Category = c;
            Init();
        }

        private void Init()
        {
            this.PartitionKey = this.Title.ToLower();
            this.RowKey = RowKey = string.Format("{0:10}_{1}", DateTime.MaxValue.Ticks - DateTime.Now.Ticks, Guid.NewGuid());
            this.rssUrls = new List<RssLink>();
            this.creationDate = DateTime.Now;
            this.Score = 0;
            this.Subscribers = 0;
            this.Description = "no description";
        }
        
        public void AddRssLink(string url)
        {
            RssLink link = new RssLink(this.Title, url);
            this.rssUrls.Add(link);
        }

        public void RemoveRssLink(RssLink rssLink)
        {
            if (this.rssUrls.Contains(rssLink))
            {
                this.rssUrls.Remove(rssLink);
            }
        }

        public void ScoresUp()
        {
            this.Score++;
        }

        public void ScoresDown()
        {
            this.Score--;
        }

        public void SubscribersUp()
        {
            this.Subscribers++;
        }

        public void SubscribersDown()
        {
            this.Subscribers--;
        }

        public Stream GetAgregatorAsStream(string outputFile)
        {
            List<string> links = new List<string>();
            foreach (RssLink r in this.rssUrls)
            {
                links.Add(r.Link);
            }
            SyndicationFeed feed = RssHelper.CreateAgregator(this.title,this.description,links, 10);
            RssHelper.SaveSyndicationFeedAsXml(feed, outputFile);

            FileStream stream = new FileStream(outputFile, FileMode.Open);
            return stream;
        }

        public List<RssLink> GetUrls()
        {
            return this.rssUrls;
        }


    }
}
